package com.t.bricks.bussiness.db.dao.person.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.person.PersonDao;
import com.t.bricks.bussiness.db.entity.person.Person;
import com.t.bricks.bussiness.db.mapper.person.PersonMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class PersonDaoImpl implements PersonDao<Person> {
	
	@Autowired
	private PersonMapper personMapper;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;

	/**
	 * 添加
	 * @param person
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity add(Person person) {
		int iErrCode = 7004;//错误码值
		
		try {
			int i = personMapper.add(person);
			if (i < 1) {
				Log.error("新增未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "新增数据失败！", 7001);//此时不需要回滚
			}
			
			int k = personMapper.addUser(person);
			if (k < 1) {
				iErrCode = 7002;
				throw new RuntimeException("新增关联的用户信息失败！");
			}
		} catch (Exception e) {
			Log.error("新增时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
			return new MsgEmity(false, "新增失败！", iErrCode);
		}
		
		return new MsgEmity(true, "新增成功！", person);
	}

	/**
	 * 删除
	 * @param sId
	 * @param iVersion
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity del(String sId, Integer iVersion) {
		int iErrCode = 7006;//错误码值
		
		try {
			//--删除用户--//
			int t = personMapper.delUser(sId);
			if (t < 1) {
				t = personMapper.findCountUser(sId);
				if (t > 0) {
					return new MsgEmity(false, "删除关联的用户数据失败！", 7001);//此时不需要回滚
				}
			}
			
			//--清除所有该人员与部门的关联信息--//
			int t1 = personMapper.delDepartment(sId);
			if (t1 < 1) {
				t1 = personMapper.findCountDepartment(sId);
				if (t1 > 0) {
					iErrCode = 7002;
					throw new RuntimeException("清除所有该人员与部门的关联信息失败！");
				}
			}
			
			//--删除该用户所有与权限关联信息--//
			int t2 = personMapper.delRight(sId);
			if (t2 < 1) {
				t2 = personMapper.findCountRight(sId);
				if (t2 > 0) {
					iErrCode = 7003;
					throw new RuntimeException("删除该用户所有与权限关联信息失败！");
				}
			}
			
			//--删除用户与角色的关联信息--//
			int t3 = personMapper.delRole(sId);
			if (t3 < 1) {
				t3 = personMapper.findCountRole(sId);
				if (t3 > 0) {
					iErrCode = 7004;
					throw new RuntimeException("删除用户与角色的关联信息失败！");
				}
			}
			
			//--删除人员--//
			int i = personMapper.del(sId, iVersion);
			if (i < 1) {
				Integer k = this.getiVersion(sId);	//如果删除的影响数小于1,则检查数据是否存在
				if (null != k && !iVersion.equals(k)) {		//有数据,并且版本号不一致
					iErrCode = 7005;
					throw new RuntimeException("删除人员数据失败，系统中的数据可能已经被更新！");
				}
			}
		} catch (Exception e) {
			Log.error("删除时发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
			return new MsgEmity(false, "删除数据失败！", iErrCode);
		}
		
		return new MsgEmity(true, "删除成功！", 7999);
	}

	/**
	 * 修改
	 * 注意:更新成功后对象属性iVersion值将被+1
	 *     当对象的属性值为null时不更新字段
	 * @param person
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity edit(Person person) {
		int iErrCode = 7005;//错误码值
		
		try {
			MsgEmity me = this.edit(person, false);
			if (!me.isSuccess()) {
				return me.setData((Integer)me.getData() + 100);
			}
			
			int i = personMapper.editUser(person);//关联修改用户数据,一定会有影响数,在sql中设置iVersion的值和修改时间
			if (i < 1) {
				iErrCode = 7004;
				throw new RuntimeException("更新关联的用户信息失败！");
			}
			
			person.setiVersion(person.getiVersion() +1);
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
			return new MsgEmity(false, "删除数据失败！", iErrCode);
		}
		
		return new MsgEmity(true, "修改成功！", person);
	}

	/**
	 * 检查待修改内容是否存在重复数据
	 * @param personData
	 * @return
	 */
	@Override
	public MsgEmity checkRepeatByEdit(Person personData) {
		int i = 0;
		try {
			i = personMapper.checkRepeatByEdit(personData);
			if (0 == i) {
				return new MsgEmity(false, "查询成功！没发现重复", 7007);
			}
		} catch (Exception e) {
			Log.error("检查待修改内容是否存在重复数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败，请查看异常日志！", 7008);
		}
		
		return new MsgEmity(true, "查询成功！发现重复", i);//将查询到的数量(或sql中的构造码)返回以便特殊业务要求
	}

	/**
	 * 检查待新增内容是否存在重复数据
	 * @param personData
	 * @return
	 */
	@Override
	public MsgEmity checkRepeatByAdd(Person personData) {
		int i = 0;
		try {
			i = personMapper.checkRepeatByAdd(personData);
			if (0 == i) {
				return new MsgEmity(false, "查询成功！没发现重复", 7007);
			}
		} catch (Exception e) {
			Log.error("检查待新增内容是否存在重复数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败，请查看异常日志", 7008);
		}
		
		return new MsgEmity(true, "查询成功！发现重复", i);//将查询到的数量(或sql中的构造码)返回以便特殊业务要求
	}

	/**
	 * 停用
	 * @param sId
	 * @param sModifieder
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity stop(String sId, String sModifieder) {
		int iErrCode = 7004;
		
		try {
			int i = personMapper.stop(sId, sModifieder);
			if (i < 1) {
				MsgEmity me = this.hasById(sId);//如果修改的影响数小于1,则检查数据是否存在
				Integer k = me.getData();
				if (null == k || k.intValue() == 0) {
					return new MsgEmity(false, "试图停用账号失败，数据可能已经被删除！", 7001);// 无法获取版本号,说明数据已经不存在
				}
				
				int iState = personMapper.getState(sId);
				if (2 == iState) {
					return new MsgEmity(true, "账号之前已经停用！", 7999);
				}
				
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "修改数据失败！", 7002);
			}
			
			i = personMapper.stopUser(sId, sModifieder);//关联停用用户账号
			if (i < 1) {
				iErrCode = 7003;
				throw new RuntimeException("更新关联的用户信息失败！");
			}
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
			return new MsgEmity(false, "操作请求失败！", iErrCode);
		}
		
		return new MsgEmity(true, "停用账号成功！", 7999);
	}
	
	/**
	 * 启用
	 * @param sId
	 * @param dEndTime
	 * @param sModifieder
	 * @return
	 */
	@Transactional
	@Override
	public MsgEmity start(String sId, Date dEndTime, String sModifieder) {
		int iErrCode = 7004;
		
		try {
			int i = personMapper.start(sId, dEndTime, sModifieder);
			if (i < 1) {
				MsgEmity me = this.hasById(sId);//如果修改的影响数小于1,则检查数据是否存在
				Integer k = me.getData();
				if (null == k || k.intValue() == 0) {
					return new MsgEmity(false, "试图启用账号失败，数据可能已经被删除！", 7001);// 无法获取版本号,说明数据已经不存在
				}
				
				int iState = personMapper.getState(sId);
				if (1 == iState) {
					return new MsgEmity(true, "账号处于启用状态！", 7999);
				}
				
				Log.error("修改未能得到预期影响值(预期1):", i);
				return new MsgEmity(false, "启用账号失败！", 7002);
			}
			
			i = personMapper.startUser(sId, sModifieder);//关联启用用户账号
			if (i < 1) {
				iErrCode = 7003;
				throw new RuntimeException("启用关联的用户信息失败！");
			}
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
			return new MsgEmity(false, "操作请求失败！", iErrCode);
		}
		
		return new MsgEmity(true, "启用账号成功！", 7999);
	}
	
	/**
	 * 根据部门编号查询员工
	 * @param sDeparmentId 部门编号
	 * @param iMainDeparment 仅限主部门
	 * @return
	 */
	@Override
	public MsgEmity findByDepartmentId(String sDeparmentId, Integer iMainDeparment) {
		List<Person> list = null;
		try {
			list = personMapper.findByDepartmentId(sDeparmentId, iMainDeparment);
			if (null == list) {
				Log.error("查询数据发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查询操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("查询分页数据发生异常:", e.getMessage());
			return new MsgEmity(false, "查询请求失败，请查看异常日志！", 7002);
		}
		
		return new MsgEmity(true, "查询成功！", list);
	}
	
}
